﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }
<style type="text/css">
    .div-collapse {
        float: left;
        height: 20px;
        line-height: 20px;
        padding-left: 5px;
    }
</style>
<form class="layui-form layui-form-pane" action="" id="_formid">
    <div class="layui-form-item">
        <div class="layui-btn-group">
            <button type='button' class="layui-btn layui-btn-normal layui-btn-sm" id="add"><i class="fa fa-plus"></i>新建菜单</button>
        </div>
    </div>
    <div class="layui-form-item">
        <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
    </div>
</form>

<script>
    layui.use(["treeGrid", "exLayer", "exUtils", "miniPage"], function () {
        let treeGrid = layui.treeGrid;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        var openWH = miniPage.getOpenWidthHeight();
        let $ = layui.$;

        treeGrid.render({
            elem: "#tableId",
            url: "/SysBasic/Module/GetList",
            cellMinWidth: 100,
            treeId: 'Id',   //树形id字段名称
            treeUpId: 'ParentId',   //树形父id字段名称
            treeShowName: 'FullName',   //以树形式显示的字段

            cols: [[
                { field: "FullName", title: "菜单名称" },
                { field: "UrlAddress", title: "菜单路径" },
                { field: "Icon", title: "图标", width: 100, templet: "#T_Icon" },
                { field: "OrderNo", title: "排序", width: 80 },
                { title: "操作", width: 120, align: "center", fixed: "right", templet: "#operationTpl" }
            ]]
        });

        $('#add').on('click', function () {
            add();
        });

        treeGrid.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);//field Id 和 数据库表字段 Id 要一致
                    break;
                case "del":
                    del(data.Id);
                    break;
            }
        });

        treeGrid.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
            }
        });

        function add() {
            exLayer.open("新建菜单", "/SysBasic/Module/Add", '100%', '100%', '0px', '0px', null, null);
        }

        function edit(id) {
            exLayer.open("编辑菜单", "/SysBasic/Module/Edit/" + id, '100%', '100%', '0px', '0px', null, null);
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysBasic/Module/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    //没开启分页，没确定按钮，手动刷新
                    setTimeout(function () {
                        window.location.reload();
                    }, 1500);
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }
    })
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="T_Icon">
    {{#  if(d.Icon == ''){ }}
    <span><i class="fa fa-circle-thin"></i></span>
    {{#  } else{ }}
    <span><i class="fa {{d.Icon}}"></i></span>
    {{#  } }}
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class="layui-btn layui-btn-normal layui-btn-xs" href="javascript:;" lay-event="edit" id="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" href="javascript:;" lay-event="del" id="del">删除</a>
</script>